package com.pig4cloud.pigx.common.customize.thread;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 可以软终止的线程
 *
 * @date 2022/8/16 15:13
 */
public class SoftEndThread extends Thread {
	/**
	 * 是否可继续运行
	 */
	public static boolean runnable = true;
	/**
	 * 集合
	 */
	public static final ConcurrentHashMap<String, SoftEndThread> threadMap = new ConcurrentHashMap<>();

	/**
	 * 将线程加入threadMap并调用run()
	 *
	 * @date 2022/8/16 16:38
	 */
	@Override
	public final synchronized void start() {
		threadMap.put(String.valueOf(this.getId()), this);
		// 执行线程逻辑
//		System.err.println(this.getId() + "执行开始");
		super.start();
	}

	/**
	 * 线程结束时处理
	 *
	 * @date 2022/8/16 16:42
	 */
	public final void end() {
		// 从threadMap中删除该线程对象
		threadMap.remove(String.valueOf(this.getId()));
		System.err.println("threadMap剩余线程对象:" + threadMap.size());
	}
}
